package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Dictionary;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(qw);
        if (emp==null){
            return R.error("登录失败");
        }else if (!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }else if (emp.getStatus()==0){
            return R.error("用户名已经禁用");
        }
        HttpSession session = request.getSession();
        session.setAttribute("employee",emp.getId());
        return R.success(emp);
    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().invalidate();
        return R.success("退出成功");
    }
    @PostMapping
    public R<String> add(@RequestBody Employee employee,HttpSession session){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));


//        ThreadLocal<Long> threadLocal = new ThreadLocal<>();
//        threadLocal.set(id);
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);
        employeeService.save(employee);

        return R.success("添加员工成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
//        log.info("page:{} pageSize:{} name:{}",page,pageSize,name);
        Page<Employee> employeePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotBlank(name),Employee::getName,name);
        qw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(employeePage,qw);
        return R.success(employeePage);
    }
    @PutMapping
    public R<String> updateStatus(@RequestBody Employee employee,HttpSession session){
//        employee.setUpdateTime(LocalDateTime.now());
//        Long id = (Long) session.getAttribute("employee");
//        employee.setUpdateUser(id);
        employee.setUpdateTime(null);
        employee.setUpdateUser(null);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
